import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // 启动要显示的界面
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("原生电池的调用"),
      ),
      body: JKBatteryLevel(),
    );
  }
}

class JKBatteryLevel extends StatefulWidget {
  @override
  _JKBatteryLevelState createState() => _JKBatteryLevelState();
}

class _JKBatteryLevelState extends State<JKBatteryLevel> {

  static const platform1 = const MethodChannel('com.jk/battery');

  int _batterylevel = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          RaisedButton(
              child: Text('获取剩余电量'),
              onPressed: _buildLevelInfo
          ),
          Text('电量：${_batterylevel}')
        ],
      ),
    );
  }

  void _buildLevelInfo() async {
    // 调用原生的电池信息
    final result = await platform1.invokeMethod('getBatteryInfo');
    setState(() {
      _batterylevel = result;
    });
  }
}
